package com.deuwise.config.oauth2;

import com.deuwise.common.utils.DateUtils;
import com.deuwise.common.utils.SpringContextHolder;
import com.deuwise.common.utils.authorize.LicenseCode;
import com.deuwise.system.entity.AuthorizeDo;
import com.deuwise.system.service.AuthorizeDoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by JAVA on 2018/7/24.
 * @author chenwenchao
 * @date 2019-07-03
 */
@Component
public class CheckAuthorizeCode {


    private static String name;

    private static String regionCode;

    @Value("${bootsystem.name}")
    public  void setName(String name1) {
        name = name1;
    }
    @Value("${bootsystem.region-code}")
    public  void setRegionCode(String regionCode1) {
        regionCode = regionCode1;
    }

    public static String getEffective(String code){
        Environment env= SpringContextHolder.getBean(Environment.class);
        AuthorizeDoService authorizeDoService= SpringContextHolder.getBean(AuthorizeDoService.class);
        String isDocker= env.getProperty("bootsystem.isdocker");
        if(StringUtils.isNotBlank(isDocker)){
            return  "永久";
        }
        String en = LicenseCode.getPlaintext(code);
        String[] split = en.split(",");
        System.out.println(en);
        if(split.length>=4) {
            if (split[4].equals("yongjiu")) {
                return "永久";
            }
            Date start = DateUtils.stringToDate(split[3], "yyyy-MM-dd");
            Date end = DateUtils.stringToDate(split[4], "yyyy-MM-dd");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String nowStr = formatter.format(new Date());
            Date now = DateUtils.stringToDate(nowStr, "yyyy-MM-dd");
            long daysBetween=(end.getTime()-now.getTime()+1000000)/(60*60*24*1000);
           if(daysBetween>0){
               return String.valueOf(daysBetween)+"天";
           }else{
               return "有误";
           }
        }
        return  "";
    }

    /**
     * 验证授权方法
     * @return
     * @throws Exception
     */
    public static boolean AuthorizeCode() throws Exception {

        Environment env= SpringContextHolder.getBean(Environment.class);
        AuthorizeDoService authorizeDoService= SpringContextHolder.getBean(AuthorizeDoService.class);
       String isDocker= env.getProperty("bootsystem.isdocker");
       if(StringUtils.isNotBlank(isDocker)){
          return  true;
       }
      AuthorizeDo authorizeDo= authorizeDoService.selectOne(null);
        if(authorizeDo==null||authorizeDo.getCode()==null){
            //如果授权文件都不存在，肯定还未授权直接返回false
            return false;
        }
        //获取硬盘序号
        String code = LicenseCode.getBaseCode(name,regionCode);
        //读取授权码文件，获取授权码
        String encoder = authorizeDo.getCode().trim();
        if(StringUtils.isEmpty(encoder)){
            return false;
        }
        //解析授权码，与本机的系统名称+行政区划+硬盘序号进行比较，这里仅仅只是用的字符串contains方法
        String en = LicenseCode.getPlaintext(encoder);
        if (!en.contains(code)) {
           return false;
        }
        /**
         * 判断时间是否过期（这一步必须判断）
         * 因为系统名称+行政区划+硬盘号+当前时间构成了申请码
         * 系统名称+行政区划+硬盘号+授权时间+授权截止时间构成了授权码
         * 两者的不同，仅仅在于授权码多了一个截止时间。
         * 如果仅仅判断了是否包含，那会出现一个bug，直接把申请码当做授权码就能通过验证的
         * 这里没用到授权时间，你可以加密的时候不要当前时间。
         */
        String[] split = en.split(",");
        System.out.println(en);
        if(split.length>=4) {
            if (split[4].equals("yongjiu")) {
                return true;
            }
            Date start = DateUtils.stringToDate(split[3], "yyyy-MM-dd");
            Date end = DateUtils.stringToDate(split[4], "yyyy-MM-dd");
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String nowStr = formatter.format(new Date());
            Date now = DateUtils.stringToDate(nowStr, "yyyy-MM-dd");
            if (!DateUtils.isEffectiveDate(now, start, end)) {
                return false;
            }
        }else{
            return false;
        }
        return true;
    }



}
